from twisted.internet import protocol, reactor
from twisted.python import log
import sys

from caster_protocol import CasterProtocol
import update_logic
import database
from settings import *

database.create()
players = {}

if LOG_TO_CONSOLE:
    log.startLogging(sys.stdout)
    log.addObserver(log.FileLogObserver(open(LOG_DIR,'a')).emit)
else:
    log.startLogging(open(LOG_DIR,'a'))

    
class GameFactory(protocol.ServerFactory):

    protocol = CasterProtocol
    
    def __init__(self):
        self.players = players
        update_logic.init(self)
        
    def get_players(self, loc_id):
        ''' returns all players for specified location '''
        return [p for p in self.players.values() if p.loc_id == loc_id]


reactor.listenTCP(LISTEN_PORT, GameFactory()) #@UndefinedVariable
reactor.run() #@UndefinedVariable
